package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.C;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Classname CommentService
 * @Description TODO 评论，点赞 业务层
 * @Date 2021/3/13 17:17
 * @Created by gan
 */
@Service
public class CommentService {
    @Reference
    private CommentApi commentApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate redisTemplate;
    //点赞
    public Long like(String publishId) {
        Long userId = UserHolder.getUserId();
        //创建Comment
        Comment comment=new Comment();
        //设置点赞属性
        comment.setUserId(userId);
        comment.setCommentType(1);//点赞
        comment.setPubType(1);//对动态操作
        comment.setPublishId(new ObjectId(publishId));
        //调用api添加评论信息，获取最新点赞数
      Long total=  commentApi.save(comment);
        String key = "publish_like_" + userId+"_" + publishId;
        //记录下点赞
        redisTemplate.opsForValue().set(key,"1");
        return total;
    }

    //取消点赞
    public Long unlike(String publishId) {
        Long userId = UserHolder.getUserId();
        //创建Comment
        Comment comment=new Comment();
        //设置删除条件
        comment.setPublishId(new ObjectId(publishId));
        comment.setUserId(userId);
        comment.setPubType(1);
        comment.setCommentType(1);
        //调用aip删除
        Long total=commentApi.remove(comment);
        String key = "publish_like_" + userId+"_" + publishId;
        //移除redis中的记录
        redisTemplate.delete(key);
        return total;
    }


    //喜欢
    public Long love(String publishId) {
        Long userId = UserHolder.getUserId();
        //创建comment
        Comment comment= new Comment();
        //设置点赞属性
        comment.setUserId(userId);
        comment.setPubType(1);//对动态操作
        comment.setCommentType(3);//点赞
        comment.setPublishId(new ObjectId(publishId));
        //调用api添加评论信息，获取最新点赞数
        Long total = commentApi.save(comment);
        String key = "publish_love_" + userId+"_" + publishId;
        // 记录下点了赞了
        redisTemplate.opsForValue().set(key,"1");
        return total;
    }


    //取消喜欢
    public Long unlove(String publishId) {
        Long userId = UserHolder.getUserId();
        //创建comment
        Comment comment=new Comment();
        //设置删除条件
        comment.setPublishId(new ObjectId(publishId));
        comment.setUserId(UserHolder.getUserId());
        comment.setCommentType(3);
        comment.setPubType(1);
        // 调用api删除
        long total = commentApi.remove(comment);
        String key = "publish_love_" + userId+"_" + publishId;
        // 移除redis中记录
        redisTemplate.delete(key);
        return total;
    }


    //动态评论列表分页查询
    public PageResult<CommentVo> findPage(String movementId, int page, int pagesize) {
        //1，调用api,PageResult中封装的都是comment对象
        PageResult<Comment> pageResult=commentApi.findPage(movementId,page,pagesize);
        //2，获取page对象中的数据列表  List<Comment>
        List<Comment> commentList = pageResult.getItems();
        //3,一个Comment对象转化为一个vo对象
        List<CommentVo> commentVoList=new ArrayList<CommentVo>();
        if(commentList!=null&&commentList.size()>0){
            for (Comment comment : commentList) {
                CommentVo commentVo=new CommentVo();
                BeanUtils.copyProperties(comment,commentVo);
                //4，查询发送评论用户详情
                UserInfo userInfo = userInfoApi.findByUserId(comment.getUserId());
                BeanUtils.copyProperties(userInfo,commentVo);
                //补全
                commentVo.setId(comment.getId().toHexString());
                commentVo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy年MM月dd日 HH:mm"));
                //commentVo.setHasLiked(0);//是否点赞
                String key = "comment_like_" + comment.getUserId()+"_" + comment.getPublishId().toHexString();
                // 记录下点了赞了
                if(redisTemplate.hasKey(key)){
                    commentVo.setHasLiked(1);//是否点赞
                }
                else {
                    commentVo.setHasLiked(0);//是否点赞
                }
                commentVoList.add(commentVo);
            }
        }
        //5，构造返回值
        PageResult<CommentVo>voPageResult =new PageResult<CommentVo>();
        BeanUtils.copyProperties(pageResult,voPageResult);
        voPageResult.setItems(commentVoList);
        return voPageResult;
    }


    //发表评论
    public long add(Map<String, String> paramMap) {
        //获取评论内容
        String content = paramMap.get("comment");
        //动态id
        String publishId = paramMap.get("movementId");
        //构建comment
        Comment comment=new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(2);
        comment.setPubType(1);
        comment.setContent(content);
        comment.setUserId(UserHolder.getUserId());
        //调用api
        long count=commentApi.save(comment);
        return count;
    }


    //评论点赞
    public Long likeComment(String commentId) {
        Long userId = UserHolder.getUserId();
        //创建comment
        Comment comment=new Comment();
        //设置点赞属性
        comment.setUserId(userId);
        comment.setCommentType(1);//点赞
        comment.setPubType(3);//对评论操作
        comment.setPublishId(new ObjectId(commentId));
        //调用api添加评论信息，且获得最新点赞数
        Long total = commentApi.save(comment);
        String key="comment_like_"+userId+"_"+commentId;
        //记录下已经点赞
        redisTemplate.opsForValue().set(key,"1");
        return total;
    }


    //评论取消点赞
    public Long unlikeComment(String commentId) {
        Long userId = UserHolder.getUserId();
        //创建comment
        Comment comment=new Comment();
        //设置删除条件
        comment.setUserId(userId);
        comment.setCommentType(1);//点赞
        comment.setPubType(3);//对评论操作
        comment.setPublishId(new ObjectId(commentId));
        //调用api删除
        Long total = commentApi.remove(comment);
        return total;
    }
}
